import 'dart:async';
import 'package:flutter/material.dart';
import 'package:mine_sweeper/models/score.dart';
import 'package:mine_sweeper/repository/scoredb_repository.dart';
import 'package:mine_sweeper/screens/home_screen.dart';

class ResultWidget extends StatefulWidget implements PreferredSizeWidget {
  final bool? win;
  final Function()? onRestart;
  final int amntOfBombs;

  ResultWidget({
    required this.win,
    required this.onRestart,
    required this.amntOfBombs,
  });

  @override
  _ResultWidgetState createState() => _ResultWidgetState();

  @override
  Size get preferredSize => Size.fromHeight(120);
}

class _ResultWidgetState extends State<ResultWidget> {
  int time = 0;
  late String difficulty = widget.amntOfBombs == 40
      ? "普通"
      : widget.amntOfBombs == 70
          ? "困难"
          : "简单";

  Timer _timer = Timer(Duration(seconds: 1), () {});

  @override
  void initState() {
    super.initState();

    countTime();
  }

  void countTime() {
    time = 0;
    _timer = Timer.periodic(
      Duration(seconds: 1),
      (timer) {
        setState(() {
          time += 1;
        });
      },
    );
  }

  Color? _getColor() {
    if (widget.win == null) {
      return Colors.yellow;
    } else if (widget.win == true) {
      return Colors.green[300];
    } else {
      return Colors.red[300];
    }
  }

  String _getIcon() {
    if (widget.win == null) {
      return 'assets/images/31.png';
    } else if (widget.win == true) {
      Score score = Score(
        seconds: time,
        difficulty: difficulty,
      );

      var repository = ScoreDBRepository();
      repository.insert(score);

      _timer.cancel();
      return 'assets/images/543.png';
    } else {
      _timer.cancel();
      return 'assets/images/421.png';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromARGB(255, 255, 255, 255),
      child: Container(
        height: 120,
        padding: EdgeInsets.fromLTRB(
            10, MediaQuery.of(context).padding.top + 10, 10, 10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            IconButton(
              onPressed: () {
                Navigator.of(context).push(
                    MaterialPageRoute(builder: (context) => HomeScreen()));
              },
              icon: Icon(Icons.arrow_back),
            ),
            Container(
              child: Center(
                child: Image.asset(
                  _getIcon(),
                  width: 30,
                ),
              ),
            ),
            InkWell(
              onTap: () {
                countTime();
                widget.onRestart!();
              },
              child: Container(
                child: Row(
                  children: [
                    Icon(Icons.refresh),
                    Text('重新开始'),
                  ],
                ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(right: 10),
              child: Text(
                "用时${time.toString()} s",
                style: TextStyle(
                    color: Color.fromARGB(255, 6, 6, 6),
                    fontWeight: FontWeight.bold),
              ),
            )
          ],
        ),
      ),
    );
  }
}
